﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using DomainModel.Entities;
using System.Web.Mvc;
using System.Web.Routing;
namespace WebUI.Helpers
{
    public static class ImageHelper
    {
        /*
        public static string ImageItemArtist(this HtmlHelper helper, Picture picture)
        {
            var sb = new StringBuilder();
            TagBuilder tag = new TagBuilder("img");
            var appRoot = HttpContext.Current.Request.ApplicationPath;
            string path ="/Content/Pictures/" + picture.PictureFileName;            
            return String.Format("<img alt='{0}' height='200' src='{0}' width='180'></img>", picture.PictureAltText, path);
            string filePath =appRoot +"Content/Pictures/" + picture.PictureFileName;
            tag.MergeAttribute("src", path, true);
            tag.MergeAttribute("height", "200", true);
            tag.MergeAttribute("width", "180", true);
            tag.MergeAttribute("alt", picture.PictureCaption.ToString(), true);
            sb.Append(tag);
            return sb.ToString();
        }
        */
        public static string Image(this HtmlHelper helper, Picture picture, object htmlAttributes)
        {
            TagBuilder builder = new TagBuilder("img");
            string url = "/Content/images/no-image.jpg";

            if (picture != null && picture.PictureID > 0)
            {
                url = "/Content/Pictures/" + picture.PictureFileName;
                builder.Attributes.Add("src", url);
                builder.Attributes.Add("id", picture.PictureFileName);
                builder.Attributes.Add("name", picture.PictureFileName);
                builder.Attributes.Add("alt", picture.PictureAltText);
            }
            else
            {
                builder.Attributes.Add("src", url);
                builder.Attributes.Add("id", "");
                builder.Attributes.Add("name", "");
                builder.Attributes.Add("alt", "");
            }


            builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            return builder.ToString(TagRenderMode.SelfClosing);
        }

    }
}
